/*
 * @lc app=leetcode.cn id=59 lang=cpp
 *
 * [59] 螺旋矩阵 II
 */
#include "include.h"
// @lc code=start
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> res(n, vector<int>(n, 0));

        int topLimit = 0;
        int bottomLimit = n-1;
        int leftLimit = 0;
        int rightLimit = n-1;

        int counter = 1;
        while (counter<=n*n){
            for (int i=leftLimit;i<=rightLimit;i++){
                res[topLimit][i] = counter;
                counter++;
            }
            topLimit += 1;
            for (int i=topLimit;i<=bottomLimit;i++){
                res[i][rightLimit] = counter;
                counter++; 
            }
            rightLimit -= 1;
            for (int i=rightLimit;i>=leftLimit;i--){
                res[bottomLimit][i] = counter;
                counter++;
            }
            bottomLimit -= 1;
            for (int i=bottomLimit;i>=topLimit;i--){
                res[i][leftLimit] = counter;
                counter++;
            }
            leftLimit += 1;
        }

        return res;
    }
    // void plotMatrix(vector<vector<int>>& res){
    //     printf("---------\n");
    //     for (int i=0;i<res.size();i++){
    //         for (int j=0;j<res[0].size();j++){
    //             printf("%d ", res[i][j]);
    //         }
    //         printf("\n");
    //     }
    // }
};
// @lc code=end

